package com.liangzhong.message.tcp
{
	import com.common.utils.Debug;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.model.AppData;
	import com.socket.clientsocket.ClientSocket;
	import com.socket.clientsocket.IClientSocket;
	import com.socket.clientsocket.IClientSocketHandler;
	
	import flash.events.Event;
	import flash.utils.ByteArray;
	
	import org.puremvc.as3.interfaces.IFacade;

	/**
	 * 主socket 接口  负责控制socket，各游戏命令发送
	 * @author lxh
	 * 
	 */	
	public class GameSocket implements IClientSocketHandler
	{
		private static var instance :GameSocket;
		
		private var _clientSocket :ClientSocket;
		
		private var _facade :IFacade;
		
		public function GameSocket()
		{
			_clientSocket = new ClientSocket();
			_clientSocket.setClientSocketCallBack(this);
		} 
		/**
		 *获取单例 
		 * @return 
		 * 
		 */		
		public static function getInstance():GameSocket
		{
			if(instance == null)
			{
				instance = new GameSocket();
			}
			return instance;
		}
		/**
		 *获取clientSocket  
		 * @return 
		 * 
		 */		
		public function get clientSocket():ClientSocket
		{
			return _clientSocket;
		}
		/**
		 * 单例 facade
		 * @param value
		 * 
		 */		
		public function set facade(value :IFacade):void
		{
			_facade = value;
		}
		/**
		 * 单例 facade
		 * @return 
		 * 
		 */		
		public function get facade():IFacade
		{
			return _facade;
		}
		/**
		 * 连接上
		 * @param errorCode
		 * @param errorDesc
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		public function onSocketConnect(errorCode:int, errorDesc:String, clientSocket:IClientSocket):Boolean
		{
			//var uid :int = AppData.getInstance().user.uid;
			//var sid :String = AppData.getInstance().user.key;
			//GameFacade.getInstance().sendNotification(ApplicationConstants.Login_login,{userId:uid,userKey:sid});
			AppData.getInstance().isConnect = true;
			facade.sendNotification(NotificationConstants.Login_Connect_Server_Success);
			return true;
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			return false;
		}
		/**
		 * 关闭
		 * @param clientSocket
		 * @param closeByServer
		 * @return 
		 * 
		 */		
		public function onSocketClose(clientSocket:IClientSocket, closeByServer:Boolean):Boolean
		{
			AppData.getInstance().isConnect = false;
			facade.sendNotification(NotificationConstants.Login_Server_Close);
			return true;
		}
		/**
		 *socke 错误信息 
		 * @param clientSocket
		 * @param e
		 * @return 
		 * 
		 */		
		public function onSocketError(clientSocket:IClientSocket, e:Event):Boolean
		{
			Debug.log("" + e);
			return true;
		}
	}
}